<template>
	<view>
		
		<view class="user-vip">
			<view class="info-img">
				<image class="user-img" :src="user.avatar" mode="aspectFit"></image>
			</view>
			<view class="info">
				<view class="user-info">
					<text>{{user.nickname}}</text>
					<!-- <text>{{user.msg}}</text> -->
					<!-- <text>{{user.status}}</text> -->
				</view>
				<view class="vip-info">
					<text>{{vipInfo.msg}}</text>
					<!-- <text>{{vipInfo.status}}</text> -->
				</view>
				<view class="login-again">
					<text  @click="login">重新登录</text>
				</view>
			</view>
			
		</view>
		
	
		
		

		<uni-table border stripe emptyText="暂无更多数据" type="selection" @selection-change = "selectChange" ref="table">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th align="center" width="100rpx">基金代码</uni-th>
				<uni-th align="center" width="100rpx">基金名称</uni-th>
				<uni-th align="center" width="100rpx">今日策略</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="(item,index) in zixuanList" :key = "index" >
				<uni-td align="center">{{item.code}}</uni-td>
				<uni-td align="center">{{item.name}}</uni-td>
				<uni-td align="center">{{item.idea}}</uni-td>
			</uni-tr>
		</uni-table>
		
		<!-- 添加基金 -->
		<uni-forms :modelValue="jijin" style="margin-top: 30rpx;">
			<uni-forms-item label="基金名称" name="name">
				<uni-easyinput type="text" v-model="jijin.name" disabled="" />
			</uni-forms-item>
			<uni-forms-item label="基金代码" name="code">
				<uni-easyinput type="text" v-model="jijin.code" placeholder="请输入基金代码" @input="add" />
			</uni-forms-item>
		</uni-forms>
		<view class="buts">
			<button class="add-btn" @click="addZixuan">添加自选基金</button>
			<button class="del-btn" @click="deleteZixuan">删除选中基金</button>
		</view>
		
	</view>
</template>

<script>
	var _self

	export default {
		data() {
			return {
				jijin: {
					code: "",
					name: "",
					idea: "未知"
				},
				vipInfo: {},
				query: {
					authtoken: "",
					alipayid: ""
				},
				user: {},
				zixuanList: [],
				select: []
			}
		},
		// 下拉刷新触发
		onPullDownRefresh() {
			this.getList()
			// 停止当前页面下拉刷新
			uni.stopPullDownRefresh()
		},
		async onLoad() {

			_self = this

			// 判断是否登录
			_self.user = uni.getStorageSync("lianghua_user")
			_self.vipInfo = uni.getStorageSync("lianghua_vip")
			if (_self.user === null || _self.user === "" || _self.vipInfo === null || _self.vipInfo === "") {
				// 未登录
				uni.showModal({
					title: '提示',
					content: '用户未登录！是否前往登录',
					success: function(res) {
						if (res.confirm) {
							_self.login()
						} else if (res.cancel) {
							// 获取自选
							_self.getList()
						}
					}
				});
			} else {
				_self.query.authtoken = _self.user.authtoken
				_self.query.alipayid = _self.user.alipayid
				// 获取自选
				this.getList()
			}

		},
		methods: {
			async getList(){
				uni.showLoading({
					title: '查询中...',
					mask: false
				})
				// 获取自选
				var result = await _self.lianghua.getList(_self.query)
				if(result !== null && result !== "" && result.length > 0){
					result.forEach(item => {
						if(item.idea === 0){
							item.idea = "未知"
						}else if(item.idea === 1){
							item.idea = "观望"
						}else if(item.idea === 2){
							item.idea = "买入"
						}else if(item.idea === 3){
							item.idea = "卖出"
						}else{
							item.idea = "未知"
						}
					})
				}
				_self.zixuanList = result
				
				uni.hideLoading()
			},
			async login(){
				uni.showLoading({
					title: '登录中...',
					mask: false
				})
				await _self.lianghua.login()
				_self.user = uni.getStorageSync("lianghua_user")
				_self.query.authtoken = _self.user.authtoken
				_self.query.alipayid = _self.user.alipayid
				_self.vipInfo = await _self.lianghua.getVipInfo(_self.query)
				uni.hideLoading()
				// 获取自选
				this.getList()
			},
			// 监听输入的基金代码
			async add(e) {
				if (e.length === 6) {
					uni.showLoading({
						title: '查询中...',
						mask: false
					})
					// 满6位后查询基金名称
					var name = await _self.store.getNameByCode(e)
					if (name === null ||
						name === undefined ||
						name.length === 0
					) {
						uni.showModal({
							content: '请输入正确的基金代码！',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									// console.log('用户点击确定');
									_self.jijin.code = ''
									_self.jijin.name = ''
								} else if (res.cancel) {
									// console.log('用户点击取消');
								}
							}
						});
					} else {
						_self.jijin.name = name
					}
					uni.hideLoading()
				}
			},
			async addZixuan(){
				if(_self.jijin.code === "" || _self.jijin.code === undefined || _self.jijin.code === null ||
				_self.jijin.name === "" || _self.jijin.name === undefined || _self.jijin.name === null ){
					uni.showModal({
						content: '请填写完整信息！',
						showCancel: false,
					});
				}else{
					uni.showLoading({
						title: '添加中...',
						mask: false
					})
					var result = await _self.zixuan.add(_self.jijin)
					uni.hideLoading()
					if(result === 'ok'){
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						});
						// 深拷贝一份
						var addItem = JSON.parse(JSON.stringify(_self.jijin)) 
						await _self.zixuanList.unshift(addItem)
						_self.jijin.code = ''
						_self.jijin.name = ''
						// this.getList()
					}else {
						uni.showToast({
						    title: '添加失败',
						    duration: 2000
						});
						this.getList()
					}
				}
				
			},
			deleteZixuan(item){
				var ids = []
				
				if(_self.select.length === 0 || _self.select === null){
					return
				}else {
					_self.select.forEach(item => {
						ids.push(_self.zixuanList[item].code)
					})
				}
				
				uni.showModal({
					content: '确认要删除选择基金吗？',
					showCancel: true,
					success: async function(res) {
						if (res.confirm) {
							console.log(ids)
							uni.showLoading({
								title: '删除中...',
								mask: false
							})
							await _self.zixuan.deleteByIds(ids)
							// 移除删除的元素
							_self.zixuanList.forEach(item => {
								if(ids.indexOf(item.code) !== -1){
									_self.zixuanList.splice(_self.zixuanList.indexOf(item),1);
								}
							})
							_self.$refs.table.clearSelection()
							uni.hideLoading()
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
				
			},
			selectChange(e){
				this.select = e.detail.index
			}
		}
	}
</script>

<style>

	@import url("lianghua.css");

</style>
